const request = require('supertest');
const app = require('./app');
const { getAllUsers } = require('./database');

describe('POST /api/users', () => {
  it('应成功创建新用户', async () => {
    const response = await request(app)
      .post('/api/users')
      .send({ nickName: 'Craft', email: 'craft@example.com' });

    expect(response.status).toBe(201);
    expect(response.body).toHaveProperty('id');
    expect(response.body.nickName).toBe('Craft');
    expect(response.body.email).toBe('craft@example.com');

    // 验证数据库中的数据
    const users = await getAllUsers();
    expect(users.some(user => user.nickName === 'Craft')).toBe(true);
  });

  it('应返回错误如果缺少必填字段', async () => {
    const response = await request(app)
      .post('/api/users')
      .send({ email: 'craft@example.com' });

    expect(response.status).toBe(400);
    expect(response.body.error).toBe('nickName 和 email 是必填字段');
  });
});